package Calculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FileCopyWindow extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JLabel label[] = new JLabel[2];
	JTextField textfield[] = new JTextField[2];
	JButton button[] = new JButton[2];
	JProgressBar progressBar = new JProgressBar();

	public FileCopyWindow() {
		setTitle("复制窗口");
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(800, 100, 500, 500);
		setLayout(null);

		String[] str = { "选择文件", "存放位置" };
		for (int i = 0; i < 2; i++) {
			label[i] = new JLabel(str[i]);
			label[i].setBounds(50, 40 + i * 50, 70, 25);
			textfield[i] = new JTextField(str[i]);
			textfield[i].setBounds(120, 40 + i * 50, 200, 25);
			textfield[i].setEditable(false);
			button[i] = new JButton("...");
			button[i].setBounds(330, 40 + i * 50, 60, 25);
			button[i].addActionListener(this);
			add(label[i]);
			add(textfield[i]);
			add(button[i]);
		}
		JButton button1 = new JButton("复制");
		JButton button2 = new JButton("取消");
		button1.setBounds(130, 250, 200, 25);
		button2.setBounds(130, 300, 200, 25);
		progressBar.setBounds(130, 200, 200, 20);
		progressBar.setStringPainted(true);
		button1.addActionListener(this);
		button2.addActionListener(this);
		add(button1);
		add(button2);
		add(progressBar);
	
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new FileCopyWindow();
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		String lable = e.getActionCommand();
		if (o == button[0]) {
			JFileChooser fc = new JFileChooser();
			int f = fc.showOpenDialog(this);
			if (f == JFileChooser.APPROVE_OPTION) {
				textfield[0].setText(fc.getSelectedFile().getPath());
			}
		} else if (o == button[1]) {
			JFileChooser fc = new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int f = fc.showOpenDialog(this);
			if (f == JFileChooser.APPROVE_OPTION) {
				textfield[1].setText(fc.getSelectedFile().getPath());

			}
		}
		if (lable.equals("复制"))
		
				handleCopy();
			
		if (lable.equals("取消"))
			handleUndo();
	}

	private void handleUndo() {
		// TODO Auto-generated method stub

	}

	private void handleCopy()  {
		File f = new File(textfield[0].getText());
		File ff=new File(textfield[1].getText());
		try {
			FileInputStream fil = new FileInputStream(f);
			FileOutputStream fos=new FileOutputStream(ff);
			
			byte[] tmp=new byte[1024];			
			while (true) {
				if(fil.available()<1024){
					int a = fil.read();
					while(a!=-1){
						fos.write(a);
						a=fil.read();
					}
					break;
				}
				fil.read(tmp);
				fos.write(tmp);
				System.out.println(fil.available());
			}
			fil.close();fos.flush();fos.close();
			System.out.println("复制完成！");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

}

}